package org.imaginedays.androidall.ui;

import org.imaginedays.androidall.R;
import org.imaginedays.androidall.thirdutils.gson.Order;
import org.imaginedays.androidall.thirdutils.gson.Result;
import org.imaginedays.androidall.types.InitInfo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;

public class GsonACT extends Activity {

	private static final String TAG = "GsonACT";

	String jsonStr = "{\"orders\":[{\"oid\":\"347\",\"status\":\"1\"},{\"oid\":\"348\",\"status\":\"1\"}],\"a\":14.15,\"b\":0}";

String initStr = "{\"init\":{\"message\":\"\",\"sessionId\":\"0be17c470b5f8072805ceda5c9f774e8\",\"needUpdate\":\"1\",\"appOpenCount\":\"10\",\"currentAppNewVersion\":\"\",\"updateUrl\":\"\",\"isVisit\":\"0\"},\"result\":\"success\",\"errormsg\":null}";
	private TextView toObj, toStr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gson);
		

		toObj = (TextView) findViewById(R.id.tv_str_to_obj);

		toStr = (TextView) findViewById(R.id.tv_obj_to_str);

		Gson gson = new Gson();
//		
//		InitInfo content  = gson.fromJson(initStr, InitInfo.class);
//		Log.e(TAG, content.init.sessionId);

		Result result = gson.fromJson(jsonStr, Result.class);

		String newStr = String.format(getString(R.string.tv_str_to_object),
				result);

		toObj.setText(newStr);

		Order order1 = new Order();
		order1.setOid(18);
		order1.setStatus(0);

		Order order2 = new Order();
		order2.setOid(19);
		order2.setStatus(0);

		Order order3 = new Order();
		order3.setOid(20);
		order3.setStatus(0);

		Result result2 = new Result();
		result2.getOrders().add(order1);
		result2.getOrders().add(order2);
		result2.getOrders().add(order3);

		
		result2.setA(45.9f);
		result2.setB(0);
	
		
		String objToStr = gson.toJson(result2);
		
		String jsonToStr = String.format(getString(R.string.tv_obj_to_str), objToStr);
		
		toStr.setText(jsonToStr);

	}
	
   public void runGson(){
	   Gson gson = new Gson();
		InitInfo content  = gson.fromJson(initStr, InitInfo.class);
		Log.e(TAG, content.init.sessionId);

   }

}
